﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class Updates : Form
    {
        public Updates()
        {
            InitializeComponent();
        }

        private int Id;

        public Updates(int id, string Title, string Author)
        {
            InitializeComponent();
            this.Id = id;
            textBoxTitle.Text = Title;
            textBoxAuthor.Text = Author;
        }

        //更新按钮
        private void button1_Click(object sender, EventArgs e)
        {
            var TTitle = string.IsNullOrEmpty(textBoxTitle.Text);//判断文章标题是否为空

            var TAuthor = string.IsNullOrEmpty(textBoxAuthor.Text);//判断文章作者是否为空

            if (TTitle)
            {
                MessageBox.Show("标题不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (TAuthor)
            {
                MessageBox.Show("作者不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (!TTitle && !TAuthor)
            {
                var res = MessageBox.Show("是否更新？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res.Equals(DialogResult.Yes))
                {
                    string sql = "UPDATE Articles SET Title = '{0}', Author = '{1}' WHERE Id = {2}";

                    sql = string.Format(sql, textBoxTitle.Text, textBoxAuthor.Text, Id);

                    var dtUpdate = DbHelper.AddOrInsertOrUpdate(sql);

                    if (dtUpdate)
                    {
                        MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败！自己找原因！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }
                else
                {
                    MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }
        //退出按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
